import { setRegion} from '../../api/index.js' 

// pages/region/region.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identity: '',
    pIndex: -1,
    cIndex: -1,
    rIndex: -1,
    provinceList: [
      '浙江',
    ],
    cityList: [
      '杭州',
      '宁波',
      '温州',
      '绍兴',
      '湖州',
      '嘉兴',
      '金华',
      '衢州',
      '台州',
      '丽水',
      '舟山'
    ],
    regionList: 
    {
      '0': [
        '上城',
        '下城',
        '江干',
        '拱墅',
        '西湖',
        '滨江',
        '萧山',
        '余杭',
        '富阳',
        '临安'
      ],
      '1': [
        '余姚',
        '慈溪',
        '奉化',
        '江东',
        '海曙',
        '鄞州',
        '江北',
        '镇海',
        '北仑'
      ],
      '2': [
        '鹿城',
        '龙湾',
        '瓯海',
        '洞头'
      ],
      '3': [
        '越城',
        '柯桥',
        '上虞'
      ],
      '4': [
        '吴兴',
        '南浔'
      ],
      '5': [
        '南湖',
        '秀洲'
      ],
      '6': [
        '婺城',
        '金东'
      ],
      '7': [
        '柯城',
        '衢江'
      ],
      '8': [
        '椒江',
        '黄岩',
        '路桥'
      ],
      '9': [
        '莲都'
      ],
      '10': [
        '定海',
        '普陀'
      ]
    },
    province: '',
    city: '',
    region: '',
    address: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {identity} = options

    this.setData({
      identity
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  bindPickerChange: function (e) {
    console.log(e)
    const { provinceList ,cityList, regionList, address, province, city, region } = this.data
    const { detail: { value }, currentTarget: { dataset: { selectAdr } } } = e


    if (selectAdr === 'province') {
      this.setData({
        pIndex: value
      },() => {
        this.setData({
          province: provinceList[this.data.pIndex]
        })
        wx.setStorageSync('province', provinceList[this.data.pIndex])
      })
    } else if (selectAdr === 'city') {
      this.setData({
        cIndex: value
      }, () => {
        this.setData({
          city: cityList[this.data.cIndex]
        })
        wx.setStorageSync('city', cityList[this.data.cIndex])
      })
    } else {
      this.setData({
        rIndex: value
      }, () => {
        this.setData({
          region: regionList[this.data.cIndex][this.data.rIndex]
        })
      })
    }

  },

  changeAdr: function(e) {
    const {detail:{value}} = e
    this.setData({
      address: value
    })
  },

  navigate: function() {
    const identity = wx.getStorageSync('identity')

    if(identity === 'SHOP') {
      wx.navigateTo({
        url: '/pages/selectService/selectService',
      })
    }else {
      wx.navigateTo({
        url: '/pages/home/home',
      })
    }
    
  },

  vaild: function() {
    const { province, city, region, address } = this.data

    if (!province) {
      return wx.showToast({
        title: '请选择省份',
        icon: 'none'
      })
    }

    if (!city) {
      return wx.showToast({
        title: '请选择城市',
        icon: 'none'
      })
    }

    if (!region) {
      return wx.showToast({
        title: '请选择区域',
        icon: 'none'
      })
    }

    if (!address) {
      return wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
    }

    return true
  },

  onSubmit: function() {
    const flag = this.vaild()
    const identity = wx.getStorageSync('identity')
    const { province, city, region, address} = this.data


    if (flag) {
      setRegion({
        identity,
        province, 
        city, 
        region, 
        address
      })
      .then(res => {
        this.navigate()
      })
      .catch(e => {
        wx.showToast({
          title: '设定区域失败',
          icon: 'none'
        })
        console.log(e, '设定区域')
      })
    }
  },

  navigateBack: function() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
  
})